package cn.yuyiling.jelly.kg.controller;

import cn.yuyiling.jelly.kg.api.KnowledgePointService;
import cn.yuyiling.jelly.kg.dto.KnowledgePointDTO;
import cn.yuyiling.jelly.kg.mapper.KnowledgePointMapper;
import cn.yuyiling.jelly.kg.mongodb.entity.KnowledgePoint;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/knowledgePoints")
public class KnowledgePointController {
    private static final Logger logger = LoggerFactory.getLogger(KnowledgePointController.class);

    @DubboReference(scope = "remote")
    private KnowledgePointService knowledgePointService;

    @PostMapping("/create")
    public KnowledgePoint createKnowledgePoint(@RequestBody KnowledgePointDTO knowledgePointDTO) {
        logger.info("Creating knowledge point with DTO: {}", knowledgePointDTO);
        KnowledgePoint knowledgePoint = KnowledgePointMapper.INSTANCE.dtoToEntity(knowledgePointDTO);
        return knowledgePointService.createKnowledgePoint(knowledgePoint);
    }

    @GetMapping("/query/{id}")
    public KnowledgePoint getKnowledgePointById(@PathVariable String id) {
        logger.info("Querying knowledge point by ID: {}", id);
        return knowledgePointService.getKnowledgePointById(id);
    }

    @GetMapping("/view/{id}")
    public KnowledgePoint viewKnowledgePointById(@PathVariable String id) {
        logger.info("view knowledge point by ID: {}", id);
        return knowledgePointService.viewKnowledgePointById(id);
    }

    @GetMapping("/view/top/{count}")
    public List<String> viewKnowledgePointRank(@PathVariable Integer count) {
        logger.info("view knowledge point rank list top: {}", count);
        return knowledgePointService.get24HourTopKnowledge(count);
    }

    /**
     * PB:
     * 注意在分布式部署的情况下，本代码可能被部署到多个节点（Pod），那么每个节点都会独立运行该定时任务，务必小心
     * 本示例多次执行该任务问题不大，如果存在多节点重复执行会有问题的情况下建议
     * 1）用redis加锁，避免多节点同时执行
     * 2）用其他定时任务机制保障该任务只被执行一次
     */
    //@Scheduled(cron = "0 0 * * * ?")
    @Scheduled(cron = "0/30 * * * * ?")
    public void refreshTop24hRanking() {
        knowledgePointService.refreshTop24hRanking();
    }



    @GetMapping("/query")
    public List<KnowledgePoint> getAllKnowledgePoints() {
        logger.info("Querying all knowledge points");
        return knowledgePointService.getAllKnowledgePoints();
    }

    @GetMapping("/query/page")
    public Page<KnowledgePoint> getKnowledgePointsByPage(Pageable pageable) {
        logger.info("Querying knowledge points with pageable: {}", pageable);
        return knowledgePointService.getKnowledgePointsByPage(pageable);
    }

    @PostMapping("/query/by_tags")
    public List<KnowledgePoint> getKnowledgePointsByTags(@RequestBody List<String> tags) {
        logger.info("Querying knowledge points by tags: {}", tags);
        return knowledgePointService.getKnowledgePointsByTags(tags);
    }

    @PostMapping("/update/{id}")
    public KnowledgePoint updateKnowledgePoint(@PathVariable String id, @RequestBody KnowledgePointDTO knowledgePointDTO) {
        logger.info("Updating knowledge point with ID: {}, DTO: {}", id, knowledgePointDTO);
        KnowledgePoint knowledgePoint = KnowledgePointMapper.INSTANCE.dtoToEntity(knowledgePointDTO);
        return knowledgePointService.updateKnowledgePoint(id, knowledgePoint);
    }

    @PostMapping("/delete/{id}")
    public void deleteKnowledgePoint(@PathVariable String id) {
        logger.info("Deleting knowledge point with ID: {}", id);
        knowledgePointService.deleteKnowledgePoint(id);
    }

    @PostMapping("/append/tags/{id}")
    public void addTagsToKnowledgePoint(@PathVariable String id, @RequestBody List<String> tags) {
        logger.info("Adding tags to knowledge point with ID: {}, tags: {}", id, tags);
        knowledgePointService.addTagsToKnowledgePoint(id, tags);
    }

    @PostMapping("/set/category/{id}")
    public void setCategoryForKnowledgePoint(@PathVariable String id, @RequestBody String category) {
        logger.info("Setting category for knowledge point with ID: {}, category: {}", id, category);
        knowledgePointService.setCategoryForKnowledgePoint(id, category);
    }



}
